from django.db import models
import datetime


# Create your models here.

class TbCutterType(models.Model):
    cutter_type = models.CharField(max_length=20, blank=True, null=True)
    modify_time = models.DateTimeField(auto_now_add=True)
    create_time = models.DateTimeField(blank=True, null=True)
    is_customer = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'tb_cutter_type'


class TbProdectStarge(models.Model):
    product_stage = models.CharField(max_length=20, blank=True, null=True)
    type = models.ForeignKey(TbCutterType, blank=True, null=True)
    modify_time = models.DateTimeField(auto_now_add=True)
    create_time = models.DateTimeField(blank=True, null=True)
    is_customer = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'tb_prodect_starge'


class TbDrawing(models.Model):
    drawing_type = models.CharField(max_length=50, blank=True, null=True)
    creator = models.CharField(max_length=30, blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)
    modify_time = models.DateTimeField(auto_now_add=True)
    cutter = models.ForeignKey(TbProdectStarge, blank=True, null=True)
    is_customer = models.IntegerField(blank=True, null=True)
    is_delete = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'tb_drawing'


class TbCustomerPaper(models.Model):
    ecn_no = models.CharField(db_column='ECN_NO', max_length=255, blank=True, null=True)  # Field name made lowercase.
    version = models.CharField(max_length=255, blank=True, null=True)
    p_name = models.CharField(max_length=255, blank=True, null=True)
    st_site = models.CharField(db_column='ST_site', max_length=255, blank=True, null=True)  # Field name made lowercase.
    drawing_type = models.CharField(max_length=255, blank=True, null=True)
    definer_name = models.CharField(max_length=255, blank=True, null=True)
    c_name = models.CharField(max_length=255, blank=True, null=True)
    c_type = models.CharField(max_length=255, blank=True, null=True)
    c_site = models.CharField(max_length=255, blank=True, null=True)
    uploader = models.CharField(max_length=255, blank=True, null=True)
    custters_spec = models.CharField(max_length=255, blank=True, null=True)
    case_name = models.DateTimeField(blank=True, null=True)
    priority = models.IntegerField(blank=True, null=True)
    alter_cause = models.TextField(blank=True, null=True)
    alter_front = models.TextField(blank=True, null=True)
    alter_later = models.TextField(blank=True, null=True)
    desc_file = models.TextField(blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)
    modify_time = models.DateTimeField(blank=True, null=True)
    upload_time = models.DateTimeField(blank=True, null=True)
    c_confirm = models.IntegerField(blank=True, null=True)
    # customer_info = models.ForeignKey(TbCustomInfo, models.DO_NOTHING, db_column='customer_info', blank=True, null=True)
    copy_id = models.IntegerField(blank=True, null=True)
    drawingtype = models.IntegerField(blank=True, null=True)
    modify_draft = models.IntegerField(blank=True, null=True)
    current = models.IntegerField(blank=True, null=True)
    enabled = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'tb_customer_paper'


class TbDrawingFile(models.Model):
    file_name = models.CharField(max_length=255, blank=True, null=True)
    file_size = models.IntegerField(blank=True, null=True)
    file_path = models.CharField(max_length=255, blank=True, null=True)
    uploader = models.CharField(max_length=255, blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)
    customer = models.ForeignKey(TbCustomerPaper, blank=True, null=True)
    file_type = models.IntegerField(blank=True, null=True)
    project = models.ForeignKey('TbProjectPaper', blank=True, null=True)
    drawing_type = models.ForeignKey(TbDrawing, blank=True, null=True)
    drawing_type_name = models.CharField(max_length=255, blank=True, null=True)
    cus = models.IntegerField(blank=True, null=True)
    # draw_type = models.CharField(max_length=255, blank=True, null=True)
    dev_type = models.CharField(max_length=255, blank=True, null=True)
    b_all = models.IntegerField(blank=True, null=True)
    sign_filepath = models.CharField(max_length=255, blank=True, null=True)
    seal_filepath = models.CharField(max_length=255, blank=True, null=True)
    file_version = models.CharField(max_length=255, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'tb_drawing_file'


class TbSeal(models.Model):
    seal_name = models.CharField(max_length=100, blank=True, null=True)
    seal_no = models.CharField(max_length=50, blank=True, null=True)
    seal_size = models.CharField(max_length=50, blank=True, null=True)
    seal_desc = models.CharField(max_length=255, blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)
    creator = models.CharField(max_length=25, blank=True, null=True)
    seal_href = models.CharField(max_length=100, blank=True, null=True)
    modify_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        managed = False
        db_table = 'tb_seal'


class TbPrintFile(models.Model):
    p_file = models.CharField(max_length=50, blank=True, null=True)
    p_reason = models.CharField(max_length=50, blank=True, null=True)
    p_num = models.IntegerField(blank=True, null=True)
    p_depar = models.CharField(max_length=50, blank=True, null=True)
    p_name = models.CharField(max_length=50, blank=True, null=True)
    p_time = models.DateTimeField(blank=True, null=True)
    drawing_file = models.ForeignKey(TbDrawingFile, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'tb_print_file'


class TbProjectPaper(models.Model):
    ecn_no = models.CharField(db_column='ECN_NO', max_length=255, blank=True, null=True)  # Field name made lowercase.
    version = models.CharField(max_length=255, blank=True, null=True)
    definer_name = models.CharField(max_length=255, blank=True, null=True)
    st_site = models.CharField(db_column='ST_site', max_length=255, blank=True, null=True)  # Field name made lowercase.
    project_name = models.CharField(max_length=255, blank=True, null=True)
    p_type = models.CharField(max_length=255, blank=True, null=True)
    custters_spec = models.CharField(max_length=255, blank=True, null=True)
    priority = models.IntegerField(blank=True, null=True)
    secrecy_level = models.IntegerField(blank=True, null=True)
    id_stage_type = models.IntegerField(blank=True, null=True)
    stage_type = models.CharField(max_length=255, blank=True, null=True)
    case_no = models.CharField(db_column='case_NO', max_length=255, blank=True, null=True)  # Field name made lowercase.
    alter_cause = models.TextField(blank=True, null=True)
    alter_front = models.TextField(blank=True, null=True)
    alter_later = models.TextField(blank=True, null=True)
    desc_file = models.TextField(blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)
    modify_time = models.DateTimeField(blank=True, null=True)
    id_cutter_type = models.IntegerField(blank=True, null=True)
    cutter_type = models.CharField(max_length=255, blank=True, null=True)
    id_copy = models.IntegerField(blank=True, null=True)
    upload_time = models.DateTimeField(blank=True, null=True)
    uploader = models.CharField(max_length=255, blank=True, null=True)
    p_confirm = models.IntegerField(blank=True, null=True)
    drawing_type = models.CharField(max_length=255, blank=True, null=True)
    id_drawing_type = models.IntegerField(blank=True, null=True)
    dev_type = models.CharField(max_length=255, blank=True, null=True)
    id_dev_type = models.IntegerField(blank=True, null=True)
    current = models.IntegerField(blank=True, null=True)
    enabled = models.IntegerField(blank=True, null=True)
    case_name = models.CharField(max_length=50, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'tb_project_paper'


class TbDrawingSeal(models.Model):
    seal = models.ForeignKey('TbSeal', blank=True, null=True, on_delete=models.DO_NOTHING)
    drawing = models.ForeignKey('TbDrawing', blank=True, null=True, on_delete=models.DO_NOTHING)
    creator = models.CharField(max_length=50, blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)
    modify_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        managed = False
        db_table = 'tb_drawing_seal'


class TbForms(models.Model):
    table_name = models.CharField(max_length=100)
    table_content = models.TextField(blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)
    # table_type = models.ForeignKey('TbFormtype', blank=True, null=True)
    creater = models.CharField(max_length=15, blank=True, null=True)
    version = models.CharField(max_length=10, blank=True, null=True)
    status = models.IntegerField(blank=True, null=True)
    desc = models.CharField(max_length=255, blank=True, null=True)
    # workflow = models.ForeignKey('Workflow', blank=True, null=True)
    modify_time = models.CharField(max_length=15, blank=True, null=True)
    sign_type = models.CharField(max_length=20, blank=True, null=True)
    layout = models.CharField(max_length=20, blank=True, null=True)


class TbApplyform(models.Model):
    name = models.CharField(max_length=50)
    card_id = models.CharField(max_length=30, blank=True, null=True)
    form = models.ForeignKey('TbForms', blank=True, null=True)
    form_content = models.TextField(blank=True, null=True)
    apply_type = models.CharField(max_length=50, blank=True, null=True)
    applicant = models.CharField(max_length=50, blank=True, null=True)
    apply_dept = models.CharField(max_length=50, blank=True, null=True)
    apply_time = models.DateTimeField(blank=True, null=True)
    form_footer = models.CharField(max_length=255, blank=True, null=True)
    sign_status = models.IntegerField(blank=True, null=True)
    wrecord = models.ForeignKey('Workflowrecord', blank=True, null=True)
    desc = models.CharField(max_length=255, blank=True, null=True)
    name = models.CharField(max_length=30, blank=True, null=True)
    apply_endtime = models.DateTimeField(blank=True, null=True)
    file1 = models.TextField(blank=True, null=True)
    file2 = models.CharField(max_length=255, blank=True, null=True)
    file3 = models.CharField(max_length=255, blank=True, null=True)
    file4 = models.CharField(max_length=255, blank=True, null=True)
    file5 = models.CharField(max_length=255, blank=True, null=True)
    file6 = models.CharField(max_length=255, blank=True, null=True)
    degree = models.CharField(max_length=20, blank=True, null=True)
    sign_image = models.CharField(max_length=20, blank=True, null=True)
    layout = models.CharField(max_length=25, blank=True, null=True)
    pdf = models.CharField(max_length=255, blank=True, null=True)
    customer = models.ForeignKey('TbCustomerPaper', blank=True, null=True)
    project = models.ForeignKey('TbProjectPaper', blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'tb_applyform'


class TbWorkflowRole2(models.Model):
    username = models.CharField(max_length=50, blank=True, null=True)

    class Meta:
        db_table = 'tb_workflow_role2'


class Duty(models.Model):
    name = models.CharField(max_length=50)
    code = models.CharField(max_length=50, blank=True, null=True)
    desc = models.CharField(max_length=500, blank=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'tb_duty'


class OrgLevel(models.Model):
    level = models.IntegerField(primary_key=True)
    level_name = models.CharField(max_length=100)
    level_code = models.CharField(max_length=50)
    duty = models.ForeignKey(Duty)

    # id = models.AutoField(unique=True)

    class Meta:
        db_table = 'tb_org_level'


class Org(models.Model):
    org_name = models.CharField(max_length=100)
    parent_id = models.IntegerField(blank=True, null=True)
    create_date = models.DateTimeField()
    org_code = models.CharField(max_length=50, blank=True, null=True)
    cost_code = models.CharField(max_length=50, blank=True, null=True)
    org_desc = models.CharField(max_length=500, blank=True, null=True)
    org_desc2 = models.CharField(max_length=500, blank=True, null=True)
    org_level = models.ForeignKey(OrgLevel)
    is_active = models.IntegerField()

    class Meta:
        db_table = 'tb_org'


class Status(models.Model):
    status_code = models.IntegerField(primary_key=True)
    status_name = models.CharField(max_length=100)
    status_desc = models.CharField(max_length=100)
    id = models.IntegerField()


class User(models.Model):
    password = models.CharField(max_length=128)
    last_login = models.DateTimeField(blank=True, null=True)
    is_superuser = models.IntegerField()
    username = models.CharField(unique=True, max_length=30)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.CharField(max_length=254)
    is_staff = models.IntegerField()
    is_active = models.IntegerField()
    date_joined = models.DateTimeField()
    nfc_id = models.CharField(db_column='NFC_id', max_length=100, blank=True, null=True)  # Field name made lowercase.
    ext = models.CharField(max_length=50, blank=True, null=True)
    short_number = models.CharField(max_length=50, blank=True, null=True)
    mobile_phone = models.CharField(max_length=50, blank=True, null=True)
    head = models.CharField(max_length=100, blank=True, null=True)
    sign = models.CharField(max_length=100, blank=True, null=True)
    status = models.ForeignKey(Status)

    class Meta:
        db_table = 'tb_user'

    def __str__(self):
        return self.last_name


class OrgRole(models.Model):
    name = models.CharField(max_length=100)
    code = models.CharField(max_length=50)
    desc = models.CharField(max_length=500)
    create_date = models.DateTimeField()

    class Meta:
        db_table = 'tb_org_role'


class UserOrg(models.Model):
    user = models.ForeignKey(User)
    org = models.ForeignKey(Org)
    create_date = models.DateTimeField()
    org_role = models.ForeignKey(OrgRole)

    class Meta:
        db_table = 'tb_user_org'


class Workflow(models.Model):
    name = models.CharField(max_length=20)
    description = models.CharField(max_length=150, blank=True, null=True)
    username = models.CharField(max_length=45, blank=True, null=True)
    effective_time = models.IntegerField(blank=True, null=True)
    end = models.IntegerField(blank=True, null=True)
    start = models.IntegerField(blank=True, null=True)
    enabled = models.IntegerField(blank=True, null=True)
    founder = models.CharField(max_length=30, blank=True, null=True)
    rectime = models.DateTimeField(blank=True, null=True)
    update_time = models.DateTimeField(blank=True, null=True)
    workflow_type = models.CharField(max_length=50)

    class Meta:
        db_table = 'tb_workflow'


class WfsAgent(models.Model):
    id = models.IntegerField(primary_key=True)
    user = models.CharField(max_length=20)
    username = models.CharField(max_length=20)
    org = models.ForeignKey(Org)
    agent = models.ForeignKey(User)
    rectime = models.DateTimeField()
    update = models.DateTimeField()
    enabled = models.IntegerField(blank=True, null=True)

    class Meta:
        db_table = 'tb_wfs_agent'


class WorkflowAgent(models.Model):
    user_agent = models.ForeignKey(WfsAgent)
    rectime = models.DateTimeField()
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    status = models.IntegerField()
    action = models.IntegerField()
    form_list = models.TextField()

    class Meta:
        db_table = 'tb_workflow_agent'


class ReminderLog(models.Model):
    no = models.CharField(max_length=50)
    rectime = models.DateTimeField()
    applicant = models.CharField(max_length=50)
    user = models.CharField(max_length=100)
    name = models.CharField(max_length=50)
    type = models.CharField(max_length=20)
    title = models.CharField(max_length=50)
    url = models.CharField(max_length=50)
    status = models.IntegerField()
    num = models.IntegerField(default=0)

    class Meta:
        db_table = 'tb_workflow_reminder_log'


class WorkflowRole(models.Model):
    role = models.CharField(max_length=30)
    desc = models.CharField(max_length=100)
    type = models.CharField(max_length=20)

    def __str__(self):
        return self.role

    class Meta:
        unique_together = (("role", "desc"),)
        db_table = 'tb_workflow_role'


class WorkflowRoleUser(models.Model):
    user_id = models.IntegerField()
    workflow_role = models.ForeignKey(WorkflowRole, related_name='user_2')

    class Meta:
        db_table = 'tb_workflow_role_user'


class Workflownode(models.Model):
    name = models.CharField(max_length=45)
    order_number = models.IntegerField(blank=True, null=True)
    property = models.TextField()
    workflow = models.ForeignKey(Workflow)

    class Meta:
        db_table = 'tb_workflownode'


class Workflowrecord(models.Model):
    no = models.CharField(db_column='NO', max_length=50, blank=True, null=True)
    pdfmode = models.CharField(max_length=200)  # Field name made lowercase.
    desc = models.CharField(max_length=200, blank=True, null=True)
    username = models.CharField(max_length=45, blank=True, null=True)
    applicant = models.CharField(max_length=45, blank=True, null=True)
    status = models.CharField(max_length=45, blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)
    workflow = models.ForeignKey(Workflow)
    pdf = models.CharField(max_length=200)
    # pdf3 = models.CharField(max_length=200)
    end_time = models.CharField(max_length=45, blank=True, null=True)
    org_id = models.IntegerField()
    signature_graph = models.IntegerField()

    class Meta:
        db_table = 'tb_workflowrecord'


class ForwardBackMode(models.Model):
    name = models.CharField(max_length=50)

    class Meta:
        db_table = 'tb_forward_back_mode'


class ForwardMode(models.Model):
    name = models.CharField(max_length=50)

    class Meta:
        db_table = 'tb_forward_mode'


class NodeGroup(models.Model):
    workflowrecord = models.ForeignKey(Workflowrecord)
    name = models.CharField(max_length=45)
    order_number = models.IntegerField()

    class Meta:
        db_table = 'tb_node_group'


class RejectMode(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=50)

    class Meta:
        db_table = 'tb_reject_mode'


class UserMode(models.Model):
    name = models.CharField(max_length=50)

    class Meta:
        db_table = 'tb_user_mode'


class NodeCase(models.Model):
    group = models.ForeignKey(NodeGroup, related_name='group')
    order_number = models.IntegerField(blank=True, null=True)
    review_result = models.CharField(max_length=100)
    approver = models.CharField(max_length=45, blank=True, null=True)
    rectime = models.DateTimeField(blank=True, null=True)
    username = models.CharField(max_length=45)
    user_choose = models.CharField(max_length=100, blank=True, null=True)
    status = models.CharField(max_length=10, blank=True, null=True)
    user_mode = models.CharField(max_length=25, db_column='user_mode', blank=True, null=True)
    forward_mode = models.CharField(max_length=25, db_column='forward_mode', blank=True, null=True)
    forward_back_mode = models.CharField(max_length=25, db_column='forward_back_mode', blank=True, null=True)
    reject_mode = models.CharField(max_length=25, db_column='reject_mode', blank=True, null=True)
    forward_list = models.CharField(max_length=100, blank=True, null=True)
    user_role_list = models.CharField(max_length=100, blank=True, null=True)
    agent = models.CharField(max_length=45, blank=True, null=True)

    class Meta:
        db_table = 'tb_node_case'

    def count(self):
        """
        Performs a SELECT COUNT() and returns the number of records as an
        integer.

        If the QuerySet is already fully cached this simply returns the length
        of the cached results set to avoid multiple SELECT COUNT(*) calls.
        """
        if self._result_cache is not None and not self._iter:
            return len(self._result_cache)
        return self.query.get_count(using=self.db)


class WorkflowApprovalRecord(models.Model):
    workflow_record = models.ForeignKey(Workflowrecord)
    status = models.CharField(max_length=20, blank=True, null=True)
    approver = models.CharField(max_length=20, blank=True, null=True)
    username = models.CharField(max_length=45, blank=True, null=True)
    next = models.CharField(max_length=20, blank=True, null=True)
    start_time = models.DateTimeField(blank=True, null=True)
    end_time = models.DateTimeField(blank=True, null=True)
    order_number = models.IntegerField(blank=True, null=True)
    action = models.CharField(max_length=20, blank=True, null=True)
    next_approver = models.CharField(max_length=100)
    desc = models.CharField(max_length=200)
    signature = models.CharField(max_length=200)
    filename = models.CharField(max_length=200)
    pdf = models.CharField(max_length=200)

    # this = models.CharField(max_length=20, blank=True, null=True)

    class Meta:
        db_table = 'tb_workflow_approval_record'

    def count(self):
        """
        Performs a SELECT COUNT() and returns the number of records as an
        integer.

        If the QuerySet is already fully cached this simply returns the length
        of the cached results set to avoid multiple SELECT COUNT(*) calls.
        """
        if self._result_cache is not None and not self._iter:
            return len(self._result_cache)
        return self.query.get_count(using=self.db)


import json


class WFSOrgRole(models.Model):
    user = models.ForeignKey(User)
    role = models.ForeignKey(WorkflowRole, related_name='user2')
    rectime = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.user.last_name + '(' + self.user.username + ')'

    class Meta:
        db_table = 'tb_wfs_org_role'


class TbDepense(models.Model):
    create_time = models.DateTimeField(blank=True, null=True)
    modify_time = models.DateTimeField(auto_now_add=True)
    customer = models.ForeignKey('TbCustomerPaper', blank=True, null=True)
    project = models.ForeignKey('TbProjectPaper', blank=True, null=True)
    accptor = models.CharField(max_length=255, blank=True, null=True)
    distributor = models.CharField(max_length=255, blank=True, null=True)
    drawing_file_id = models.IntegerField(blank=True, null=True)
    status = models.IntegerField(blank=True, null=True)
    product_stage = models.CharField(max_length=30, blank=True, null=True)
    product_type = models.CharField(max_length=30, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'tb_depense'


class TbPrinting(models.Model):
    project = models.ForeignKey('TbProjectPaper', blank=True, null=True)
    customer = models.ForeignKey(TbCustomerPaper, blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)
    signer = models.CharField(max_length=255, blank=True, null=True)
    print_no = models.IntegerField(blank=True, null=True)
    recycling_no = models.IntegerField(blank=True, null=True)
    print_status = models.IntegerField(blank=True, null=True)
    modify_time = models.DateTimeField(auto_now_add=True)
    drawing_file_id = models.IntegerField(blank=True, null=True)
    sponsor = models.CharField(max_length=255, blank=True, null=True)
    dispensing = models.CharField(max_length=255, blank=True, null=True)
    reason = models.CharField(max_length=255, blank=True, null=True)
    product_stage = models.CharField(max_length=30, blank=True, null=True)
    product_type = models.CharField(max_length=20, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'tb_printing'
